// pages/orders/address.js
var app = getApp();
Page({
  data:{
    addressInfo:{},
    addressVal:[0,0,0],
    addressData:{},
    from:false
  },
  onLoad: function (options) {
    console.log(options);
    this.setData({
      from:options.from
    })
    this.setAddress()
  },
  onReady: function () {
  
  },
  onShow: function () {
    this.setAddress()
  },
  onPullDownRefresh: function () {
  
  },
  onReachBottom: function () {
  
  },
  userAddress:function(e){
    var id = e.currentTarget.dataset.id
    for (var i = 0; i < this.addressList.length; i++) {
      if(this.addressList[i].id == id){
        app.address = this.addressList[i]
        wx.navigateBack()
      }
    }
  },
  setAddress:function(){
    var self = this;
    wx.showLoading({
        title : "正在加载",
        mask  : true,
    });
    app.request.wxRequest({
      url: 'address-list',
      success: function (res) {
        self.addressList = res
        wx.hideLoading();
        self.setData({
          address: res
        })
      }
    })
  },
  chooseWxAddress:function(){
    var self = this
    wx.chooseAddress({
      success: function (res) {
        var data = {}
        data.userName = res.userName
        data.province = res.provinceName
        data.city = res.cityName
        data.county = res.countyName
        data.address = res.detailInfo
        data.telNumber = res.telNumber
        app.request.wxRequest({
          url:'address-edit',
          data:data,
          method:'POST',
          success:function(res){
            self.setAddress()
          }
        })
      }
    })
  },
  setDefault:function(e){
    var self = this;
    var id = e.currentTarget.dataset.id;
    app.request.wxRequest({
      url:'address-edit&act=setdefault&aid='+id,
      success:function(res){
        app.showToast(self, '设置成功')
        self.setAddress()
      }
    })
  },
  delAddress:function(e){
    var id = e.currentTarget.dataset.id;
    var self = this
    app.request.wxRequest({
      url:'address-edit&act=del&aid='+id,
      success:function(res){
        app.showToast(self, '删除成功')
        self.setAddress()        
      }
    })
  },
  bindblur:function(e){
    var field = e.currentTarget.dataset.name
    var word = e.detail.value
    this.data.addressInfo[field] = word
  },
  // 跳转至编辑地址页
  editAddress: function (e) {
    var id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: '/pages/member/edit-address?id=' + id
    })
  },
  // 微信一键获取
  onKeyAddress:function(e){
    var self = this
    wx.chooseAddress({
      success: function (res) {
        console.log(res);
        var data = {}
        data.userName = res.userName
        data.province = res.provinceName
        data.city = res.cityName
        data.county = res.countyName
        data.address = res.detailInfo
        data.telNumber = res.telNumber
        // self.setData({
        //   addressData:data,
        //   addressInfo: self.data.addressInfo,
        // })
        wx.showLoading({
          title: "正在保存",
          mask: true,
        });
        app.request.wxAsk({
          url: 'address/edit',
          data:data,
          method: 'POST',
          success: function (res) {
            wx.hideLoading();
            if (res.code == 0) {
              app.address = res.data;
              wx.navigateBack()
            }
          }
        })

        
      }
    })
  }




})